package wangjg.android.wifiadmin;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class AlarmUtil {
	
	public static void setAlarm(Context context, long timeInMillis, Intent intent) {
		setAlarm(context, timeInMillis, null, intent);
	}
	
	public static void setAlarm(Context context, long triggerAtTime, Integer interval, Intent intent) {
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		if(interval!=null){
			alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, interval, getPendingIntent(context, intent));
		}else{
			alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, getPendingIntent(context, intent));
		}
	}

	public static void cancelAlarm(Context context, Intent intent){
		AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);  
		am.cancel(getPendingIntent(context, intent));  
	}
	
	private static PendingIntent getPendingIntent(Context context, Intent intent){
       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
       return pendingIntent;
	}
}
